package ThinkInJava;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 使用正则表达式
 * @author Administrator
 *
 */
public class RegularExpression {

	
	public static void main(String[] args) {

		//\w词字符，[a-zA-Z0-9]  \W，非词字符
		//\s,空格符，\S,非空格符
		//\d数字 \D非数字
		
		String inputString="http://www.baidu.com/index.html";
		String urlString="http://zhidao.baidu.com/link?url=Cn-ixB001dM2knm5mE6kn6w" +
				"SZrELtJYGrKaMnTAhWrtPqb9588BMylbn-B05SuRSYjFd0Q-1JJDu0zbmiG-pRq";
		//电子邮件匹配
		System.out.println("10_oo944770@qq.com".matches("\\w+@\\w+\\.\\w+"));
		System.out.println("10944770@qq.com".matches("\\d+@\\w+\\.\\w+"));
		//手机号码匹配
		System.out.println("10094475536".matches("1\\d{10}"));
		//域名匹配,暂时只能匹配一些简单的域名
		Pattern pattern1=Pattern.compile("(http://|https://|ftp://|www.){0,1}[a-zA-Z0-9.]+[a-zA-Z0-9]" +
				"[/a-zA-Z0-9-~?&=.]*");
		Matcher matcher1=pattern1.matcher(urlString);
		if(!matcher1.matches()){
			System.out.print("please input right domain name.Don't forget \".com\" etc\n");
		}else{
			System.out.print("You input right domain name!\n");
		}
		System.out.println("10944770@qq.com".matches("\\d+\\D+@\\w+\\.\\w+"));
		System.out.println("10944770@qq.com".matches("\\d+\\D+@\\w+\\.\\w+"));
		Pattern pattern=Pattern.compile("\\d{11}"); //像这种表示的是匹配00后面跟一个或者零个9
		Matcher matcher=pattern.matcher("100900900944755369");
		//find和Matcher有所不同，find是找到符合的，Matcher是该字段是否符合
		while(matcher.find()){
			System.out.print("match \""+matcher.group()+"\" "+"at position "+matcher.start()+"-"+(matcher.end()-1)+"\n");
		}
		System.out.println("10094475536".matches("1\\d{10}"));
		
		
	}

}
